package party.lovey.remote.client;

import party.lovey.remote.amq.Receiver;
import party.lovey.remote.amq.Sender;
import party.lovey.remote.utils.Consts;
import party.lovey.remote.utils.ImageUtil;
import party.lovey.remote.utils.RobotUtil;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by huhaichao on 2017/3/19.
 */
public class Client {
    
    public static void main(String[] args){
        run();
    }
    

    private static void run(){
        while(true){
            sendImage();
            receivePoint();
            try {
                Thread.sleep(Consts.SLEEP_MILLIS);
            } catch (InterruptedException e) {

            }
        }
    }

    /**
     * 接受远程鼠标事件
     */
    private static void receivePoint() {
        Point point = (Point) Receiver.receiveObjectMessage(Consts.MESSAGE_CLICK_POINT);
        onReceivePoint(point);
    }

    /**
     * 收到鼠标事件响应
     * @param point
     */
    private static void onReceivePoint(Point point){
        RobotUtil.clickPoint(point);
    }

    /**
     * 向服务端发送图像
     */
    public static void sendImage(){
        BufferedImage screenImage = RobotUtil.getScreenImage();
        Sender.sendBinaryMessage(Consts.DEFEALT_QUEUE_NAME,ImageUtil.toBytes(screenImage));
    }
}
